import router from './router'
import store from './store'

router.beforeEach(async(to, from, next) => {
  // start progress bar
  const whiteList = ['/login', '/404', '/testInput']
  const token = store.getters.token

  if (token && to.path === '/login') next('/')
  if (token && to.path !== '/login') {
    if (!store.state.user.userInfo.userId) {
      await store.dispatch('user/getUserInfo')
    }
    next()
  }
  if (!token && whiteList.includes(to.path)) next()
  if (!token && !whiteList.includes(to.path)) next('/login')
})
